<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="addCate">添加分类</el-button>
      <el-table :data="cateList" stripe style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="menuName" label="分类名字"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="toEditCate(scope.row)"
              >修改分类</el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="deleteCate(scope.row.menuID)"
              >删除分类</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog 
      title="添加分类" 
      :visible.sync="addDialogVisible" 
      width="50%"
      @close="addDialogClosed">
      <el-form
        :model="addCateForm"
        ref="addCateRef"
        label-width="100px"
        class="demo-ruleForm"
        :rules="addCateRules"
      >
        <el-form-item label="分类名称：" prop="menuName">
          <el-input v-model="addCateForm.menuName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddForm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改分类对话框 -->
    <el-dialog title="修改分类" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editCateForm"
        ref="editCateRef"
        label-width="100px"
        class="demo-ruleForm"
        :rules="editCateRules"
      >
        <el-form-item label="分类名称：" prop="menuName">
          <el-input v-model="editCateForm.menuName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      cateList: [],
    //   添加分类相关
        addCateForm: {
            menuName: ''
        },
        addCateRules: {
          menuName: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
        },
      addDialogVisible: false,
      //修改分类相关
      editCateForm: {},
      editCateRules: {
        menuName: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ]
      },
      editDialogVisible: false,

    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    //获取菜单分类列表
    async getCateList() {
      const { data: res} = await this.$http.get('menu')
      this.cateList = res.data
      console.log('res.data: ', res.data);
    },
    //点击添加分类按钮
    addCate() {
      this.addDialogVisible = true
    },
    //点击提交添加表格
    submitAddForm() {
      //验证表单
      this.$refs.addCateRef.validate(async valid => {
        if(!valid) return
        const { data: res } = await this.$http.post('menu', this.addCateForm)
        if(!res.flag) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addDialogVisible = false
        this.getCateList()
      })
    },
    //添加对话框关闭时
    addDialogClosed() {
      //清空表单域
        this.$refs.addCateRef.resetFields()
    },
    //点击修改按钮
    toEditCate(cate) {
      this.editDialogVisible = true
      this.editCateForm = cate
    },
    //点击提交修改
    submitEditForm() {
      this.$refs.editCateRef.validate(async valid => {
        if(!valid) return 
        const { data: res } = await this.$http.put('menu/'+ this.editCateForm.menuID,{
          menuName: this.editCateForm.menuName
        })
        if(!res.flag) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.editDialogVisible = false
        this.getCateList()
      })
    },
    //点击删除按钮
    deleteCate(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const { data: res } = await this.$http.delete(`menu/${id}`)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getCateList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }

  }
}
</script>
<style lang="less" scoped></style>
